package dataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCMensajeDAO {

private Connection conn;
	
	
	public JDBCMensajeDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
	public void finalice(){
		ConnectionManager.getInstance().checkIn(conn);
	}
	
	//insertar mensaje
	public void insertMensaje(String s){
		Connection conn = ConnectionManager.getInstance().checkOut();

		String sql = "INSERT INTO anuncios (mensaje) VALUES (?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            
            this.deleteMensaje();
            
            stmt.setString(1, s);

            stmt.executeUpdate();
            
            

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}
	
	//mostrar mensaje
	public String selectMensaje(){
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        
        String s = "";
       
        String sql = "SELECT * FROM anuncios";
        try {
            stmt = conn.prepareStatement(sql);
            //stmt.setInt(1, 1);
            result = stmt.executeQuery();

            if(result.next()){
            	
            	s+=""+result.getString("mensaje");
            	

            }
        } catch (SQLException u) {
            System.out.println("Message: " + u.getMessage());
            System.out.println("SQLState: " + u.getSQLState());
            System.out.println("ErrorCode: " + u.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException u) {
            }
        }
        return s;
	}
	
	//borra un mensaje
	private void deleteMensaje(){
		
		String sql = "DELETE FROM anuncios WHERE (id_mensaje = ?)";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, 1);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}
	
}
